package com.tensquare.base.service;

import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import entity.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * & ProjectName：  tensquare-parent
 * & PackageName:   com.tensquare.base.service
 * & Creation_Date： 2020/3/30 06:10
 * & Author：しの
 * ----------------------------------
 * & Description:
 */
@Service
public class LabelService {
    @Autowired
    private LabelDao labelDao;
    @Autowired
    private IdWorker idWorker;

    //1 查询所有标签
    public List<Label> findAll() {
        return labelDao.findAll();
    }

    //2 根据id查询label
    public Label findLabelById(String labelId) {
        return labelDao.findById(labelId).get();
    }

    //3 添加
    public void add(Label label) {
        //绑定id
        label.setId(idWorker.nextId() + "");
        labelDao.save(label);
    }

    //4 修改
    public void update(Label label) {
        labelDao.save(label);
    }

    //5 删除
    public void delete(String labelId) {
        labelDao.deleteById(labelId);
    }

    //6 条件查询
    public List<Label> search(Map searchMap) {
        //1 构造查询条件
        Specification<Label> labelSpecification = createSpecification(searchMap);
        //2 执行查询
        return labelDao.findAll(labelSpecification);
    }
    //7 条件查询+分页
    public PageResult searchByPage(Map searchMap, int page, int size) {
        //1 构造查询条件
        Specification<Label> specification = createSpecification(searchMap);
        //2 构造分页对象
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        //3 执行
        Page<Label> labelPage = labelDao.findAll(specification, pageRequest);
        //4 返回自定义分页结果类
        return new PageResult(labelPage.getTotalElements(),labelPage.getContent());

    }
    //创建查询条件的方法
    private Specification<Label> createSpecification(Map searchMap) {
        return new Specification<Label>() {
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                //1 构造查询条件的集合
                List<Predicate> predicateList = new ArrayList<>();
                //2 添加查询条件
                if (searchMap != null) {
                    //标签名称
                    if (StringUtils.isNotBlank((CharSequence) searchMap.get("labelname"))) {
                        predicateList.add(criteriaBuilder.like(root.get("labelname").as(String.class), "%" + searchMap.get("labelname") + "%"));
                    }
                    //状态是否相等
                    if (StringUtils.isNotBlank((CharSequence) searchMap.get("state"))) {
                        predicateList.add(criteriaBuilder.equal(root.get("state").as(String.class), searchMap.get("state")));
                    }
                    //recommend
                    if (StringUtils.isNotBlank((CharSequence) searchMap.get("recommend"))) {
                        predicateList.add(criteriaBuilder.equal(root.get("recommend").as(String.class), searchMap.get("state")));
                    }
                }
                //3 返回查询条件对象
                // 构造数组
                Predicate[] predicates = new Predicate[predicateList.size()];
                // 把集合的内容放入到数组中
                predicateList.toArray(predicates);
                // 返回
                return criteriaBuilder.and(predicates);
            }
        };
    }

}
